package com.lzxzjy.serivce.impl;

import com.lzxzjy.mapper.BookMapper;
import com.lzxzjy.mapper.BorrowInfoMapper;
import com.lzxzjy.pojo.Book;
import com.lzxzjy.pojo.BorrowInfo;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.serivce.BorrowInfoService;
import com.lzxzjy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class BorrowINfoServiceImpl implements BorrowInfoService {
    private final SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public boolean borrowBooks(int readerIs, List<Integer> bookIds) {
        SqlSession sqlSession = factory.openSession();
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        int integer = 0;
        for (int bookId: bookIds) {
            integer += borrowInfoMapper.insertBookByBookIdAndReaderId(readerIs, bookId);
        }
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public PageBean<BorrowInfo> selectBorrowInfoByPageAndBookTerms(BorrowInfo borrowInfo, int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        int begin = (currentPage - 1) * pageSize;
        List<BorrowInfo> borrowInfos = borrowInfoMapper.selectBorrowInfoByPageAndBookTerms(borrowInfo, begin, pageSize);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        for (BorrowInfo b : borrowInfos) {
            try {
                b.setArrears(((now.getTime() - simpleDateFormat.parse(b.getReturnTime()).getTime()) / (1000 * 60 * 60 * 24) < 0 ? 0 : (simpleDateFormat.parse(simpleDateFormat.format(now.getTime())).getTime() - simpleDateFormat.parse(b.getReturnTime()).getTime()) / (1000 * 60 * 60 * 24)) * 0.2);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        Integer borrowInfoTotalCountByTerms = borrowInfoMapper.selectBorrowInfoTotalCountByTerms(borrowInfo);
        PageBean<BorrowInfo> borrowInfoPageBean = new PageBean<>();
        borrowInfoPageBean.setRows(borrowInfos);
        borrowInfoPageBean.setTotalCount(borrowInfoTotalCountByTerms);
        return borrowInfoPageBean;
    }

    @Override
    public boolean lostBooks(int[] bookIds) {
        SqlSession sqlSession = factory.openSession();
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        borrowInfoMapper.deleteBorrowInfoByBookIds(bookIds);
        Integer integer = 0;
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        for (int bookId : bookIds) {
            Book book = bookMapper.selectBookByPageAndTerms(new Book(bookId, "%%", "%%", "%%", "%%", null, "%%", "%%", null), 0, 1).get(0);
            book.setBookState(-11);
            integer += bookMapper.updateBookById(book);
        }
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean returnBooks(int[] bookIds) {
        SqlSession sqlSession = factory.openSession();
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        borrowInfoMapper.deleteBorrowInfoByBookIds(bookIds);
        Integer integer = 0;
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        for (int bookId : bookIds) {
            Book book = bookMapper.selectBookByPageAndTerms(new Book(bookId, "%%", "%%", "%%", "%%", null, "%%", "%%", null), 0, 1).get(0);
            book.setBookState(11);
            integer += bookMapper.updateBookById(book);
        }

        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean lostBook(int bookId) {
        SqlSession sqlSession = factory.openSession();
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        Integer integer = borrowInfoMapper.deleteBorrowInfoByBookId(bookId);

        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookByPageAndTerms(new Book(bookId, "%%", "%%", "%%", "%%", null, "%%", "%%", null), 0, 1).get(0);
        book.setBookState(-11);
        integer += bookMapper.updateBookById(book);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean returnBook(int bookId) {
        SqlSession sqlSession = factory.openSession();
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        Integer integer = borrowInfoMapper.deleteBorrowInfoByBookId(bookId);

        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookByPageAndTerms(new Book(bookId, "%%", "%%", "%%", "%%", null, "%%", "%%", null), 0, 1).get(0);
        book.setBookState(11);
        integer += bookMapper.updateBookById(book);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }
}
